<template>
  <view class="goods_list">
    <view v-for="(item, id) in goodsList" :key="id" @click="gotoDetail(item)">
      <my-goods :goods="item"></my-goods>
    </view>
  </view>
</template>

<script>
import myGoods from "/src/components/my-goods/index";
export default {
  name: "goods_list",
  components: { myGoods },
  props: {},
  data() {
    return {
      queryObj: {
        query: "",
        cid: "",
        pagenum: 1,
        pagesize: 10,
      },
      goodsList: [],
      total: 0,
      isloading: false,
    };
  },
  computed: {},
  methods: {
    // 发送请求获取商品列表
    async getGoodsList(cb) {
      this.isloading = true;

      const res = await uni.$http.get(
        `/api/public/v1/goods/search`,
        this.queryObj
      );
      this.isloading = false;
      uni.hideLoading();
      cb && cb();
      if (res.statusCode !== 200) return uni.$showMessage("请求商品列表失败");

      this.goodsList = [...this.goodsList, ...res.data.message.goods];
      this.total = res.data.message.total;
    },
    // 跳转到商品详情页
    gotoDetail(item) {
      uni.navigateTo({
        url: `/subpkg/goods_detail/index?goods_id=${item.goods_id}`});
    },
  },
  watch: {},

  // 上拉加载新数据
  onReachBottom() {
    if (this.queryObj.pagesize * this.queryObj.pagenum >= this.total)
      return uni.$showMessage("数据加载完毕啦");
    if (this.isloading) return;
    this.queryObj.pagenum += 1;
    this.getGoodsList();
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.queryObj.pagenum = 1;
    this.total = 0;
    this.goodsList = [];
    this.isloading = false;

    this.getGoodsList(() => {
      uni.stopPullDownRefresh();
    });
  },

  // 页面周期函数--监听页面加载
  onLoad(options) {
    this.queryObj.query = options.query || "";
    this.queryObj.cid = options.cid || "";

    this.getGoodsList();
  },
  // 页面周期函数--监听页面初次渲染完成
  onReady() {},
  // 页面周期函数--监听页面显示(not-nvue)
  onShow() {},
  // 页面周期函数--监听页面隐藏
  onHide() {},
  // 页面周期函数--监听页面卸载
  onUnload() {},
  // 页面处理函数--监听用户下拉动作
  // onPullDownRefresh() { uni.stopPullDownRefresh(); },
  // 页面处理函数--监听用户上拉触底
  // onReachBottom() {},
  // 页面处理函数--监听页面滚动(not-nvue)
  // onPageScroll(event) {},
  // 页面处理函数--用户点击右上角分享
  // onShareAppMessage(options) {},
};
</script>

<style scoped lang="scss">
.goods_list {
}
</style>